<?php
namespace App\Http\Controllers\AppApi\Member;

use App\Jobs\Fun;
use App\Services\Admin\JobService;
use App\Utilities\Upload;
use Illuminate\Http\Request;
use App\Services\Admin\StaffService;

class StaffController extends BaseController
{
    /**
     * 员工接口控制器
     * 制作者：九哥
     */

    protected $staffService;

    public function __construct()
    {
        parent::__construct();
        $this->staffService = new StaffService();
    }

    /**
     * ---------------------------
     * 员工必选信息
     * ---------------------------
     */

    /**
     * 同步员工信息1
     * 待删除
     */
    public function addComInfo(Request $request)
    {

        if ($request->app_access!=$this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $viewJump = $request->viewJump;
        if (!$token) {
            return redirect('/h5/error.html?errMsg=用户登陆错误');
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return redirect('/h5/error.html?errMsg=您未注册或未登录');
        }
        if (!isset($request->staffXxz)) {
            return redirect('/h5/error.html?errMsg=文件没有上传');
        }
        //创建目录
        if (!is_dir('uploads/'.date('Ymd', time()))) {
            exec('mkdir uploads/'.date('Ymd', time()));
        }
        $model = $this->staffService->getOneByUid($uid);
        $resFile = Upload::uploadImgByApi($request,'staffXxz');
        if ($resFile['code']!=0) {
            return redirect('/h5/error.html?errMsg='.$resFile['msg']);
        }
        if ($model && $model->image &&
            file_exists(ltrim($model->image,'/'))) {
            unlink(ltrim($model->image,'/'));
        }
        $image = $resFile['filePath'];
        $type = $request->type;
        $cate = $request->cate;
        $jobType = $request->staffJob;
        $realname = $request->staffName;
        $sex = $request->staffSex;
        $idcard = $request->idcaid;
        $birth = $request->staffBirth;
        $edu = $request->staffEdu;
        $weight = $request->staffWeight;
        $polity = $request->staffPolity;
        $origin = $request->staffOrigin;
        $nation = $request->staffNation;
        $marry = $request->staffMarry;
        $house = $request->staffHouse;
        if ($type!=1) {
            return redirect('/h5/error.html?errMsg=主体身份错误');
        }
        if (!in_array($cate,[1,2])) {
            return redirect('/h5/error.html?errMsg=员工类型错误');
        }
        if (!$sex || !$birth || !$edu || !$polity || !$marry) {
            return redirect('/h5/error.html?errMsg=性别/出生/学历/政治/婚姻未选择');
        }
        $dataArr = array(
            'uid' => $uid,
            'cate' => $cate,
            'realname' => $realname ? $realname : '',
            'sex' => $sex,
            'birthday' => $birth ? $birth : '',
            'education' => $edu,
            'weight' => $weight ? $weight : '',
            'political_status' => $polity ? $polity : '',
            'native_place' => $origin ? $origin : '',
            'nationality' => $nation ? $nation : '',
            'marriage' => $marry,
            'idcard' => $idcard ? $idcard : '',
            'house' => $house,
            'image' => $image,
            'job' => $jobType,
        );
        $this->staffService->add($uid,$dataArr);
        return redirect($viewJump);
    }

    /**
     * 获取员工一条信息
     */
    public function getShow(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->staffService->getOneByUid($uid);
        $skills = $this->staffService->getSkills($uid);
        $hobbys = $this->staffService->getHobbys($uid);
        if (!$skills || !$hobbys) { return $this->toJson(-16); }
        if (!$model) {
            $dataArr = array(
                'skillStr' => '',
                'hobbyStr' => '',
                'skills' => $skills,
                'hobbys' => $hobbys,
            );
            return $this->toJson(0,$dataArr);
        }
        $sexName = '';
        if ($sex=$model->sex) {
            $sexName = $sex==1 ? '男' : '女';
        }
        $weight = $model->weight ? $model->weight : '';
        $polityName = $this->staffService->getPolityName($model->political_status);
        $dataArr = array(
            'realname' => $model->realname,
            'sex' => $model->sex,
            'sexName' => $sexName,
            'birth' => $model->birthday,
            'edu' => $model->education,
            'eduName' => $this->staffService->getEduName($model->education),
            'weight' => $weight,
            'polityName' => $polityName,
            'polity' => $model->political_status,
            'origin' => $model->native_place,
            'nation' => $model->nationality,
            'marry' => $model->marriage,
            'marryName' => $this->staffService->getMarryName($model->marriage),
            'idcard' => $model->idcard,
            'house' => $model->house,
            'image' => $model->image,
            'houseAddress' => $model->household_address,
            'liveAddress' => $model->residential_address,
            'contactPerson' => $model->contact_person,
            'contactMobile' => $model->contact_phone,
            'salary' => $model->salary,
            'skillStr' => $model->skill,
            'hobbyStr' => $model->hobby,
            'skills' => $skills,
            'hobbys' => $hobbys,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 员工资料更新
     * /api/member/staff/modify
     */
    public function setModify(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        if (!$token) { return $this->toJson(-5); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        //获取参数
        $type = $request->type;
        $cate = $request->cate;
        $infos = $request->staffInfos;
        $realname = $infos['realname'];
        $job = $infos['jobType'] ?? 0;
        $sex = $infos['sex'];
        $birth = $infos['birth'];
        $edu = $infos['edu'];
        $weight = $infos['weight'];
        $polity = $infos['polity'];
        $origin = $infos['origin'];
        $nation = $infos['nation'];
        $marry = $infos['marry'];
        $idcard = $infos['idcard'];
        $house = $infos['house'];
        $xxz = $infos['xxz'];
        $address = $infos['address'];
        $live = $infos['live'];
        $person = $infos['person'];
        $mobile = $infos['mobile'];
        $salary = $infos['salary'];
        $skillStr = $infos['skillStr'];
        $hobbyStr = $infos['hobbyStr'];
        if (!$cate || !$realname || !$sex || !$birth || !$edu ||
            !$weight || !$polity || !$origin || !$nation ||
            !$marry || !$idcard || !$house || !$xxz || !$address ||
            !$live || !$person || !$mobile || !$salary || !$skillStr
            || !$hobbyStr) {
            return $this->toJson(-2);
        }

        if(!Fun::checkDateIsValid($birth)){
            return $this->toJson(-2,[],'日期格式错误');
        }
        if(!is_numeric($salary)){
            return $this->toJson(-2,[],'薪水输入错误');
        }
        //var_dump(is_numeric($salary),$salary);exit;

        $dataArr = array(
            'cate' => $cate,
            'realname' => $realname,
            'job' => $job,
            'sex' => $sex,
            'birthday' => $birth,
            'education' => $edu,
            'weight' => $weight,
            'political_status' => $polity,
            'native_place' => $origin,
            'nationality' => $nation,
            'marriage' => $marry,
            'idcard' => $idcard,
            'house' => $house,
            'image' => $xxz,
            'household_address' => $address,
            'residential_address' => $live,
            'contact_person' => $person,
            'contact_phone' => $mobile,
            'salary' => $salary,
            'skill' => $skillStr,
            'hobby' => $hobbyStr,
            'uid' => $uid,
        );
        $this->staffService->add($dataArr,$uid);
//        $this->staffService->modifyByUid($dataArr,$uid);
        return $this->toJson(0);
    }

    /**
     * ---------------------------
     * 员工必选信息
     * ---------------------------
     */


    /**
     * ---------------------------
     * 员工可选信息
     * ---------------------------
     */

    /**
     * 员工公司信息2，获取一条记录
     */
    public function getShow2(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->staffService->getOptionByUid($uid);
        if (!$model) { return $this->toJson(-16); }
        $datatArr = array(
            'eduBgs' => $model['eduBgs'],
            'workHistorys' => $model['workHistorys'],
            'familys' => $model['familys'],
            'comments' => $model['comments'],
        );
        return $this->toJson(0,$datatArr);
    }

    /**
     * 员工公司信息2，选项数据
     */
    public function getComRadioList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $models = $this->staffService->getComRadios();
        $dataArr = array(
            'jobTypes' => $models['jobTypes'],
            'skills' => $models['skills'],
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 员工公司信息2，选项数据更新
     */
    public function setOptionModify(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $infos = $request->staffOptions;
        $this->staffService->setOptionModify($uid,$cate,$infos);
        return $this->toJson(0);
    }

    /**
     * ---------------------------
     * 员工可选信息
     * ---------------------------
     */


    /**
     * ---------------------------
     * 员工公司求职意向
     * ---------------------------
     */

    /**
     * 获取求职意向
     */
    public function getComWill(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $model = $this->staffService->getComWill($uid);
        return $this->toJson(0,$model);
    }

    /**
     * 设置求职意向
     */
    public function setComWill(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $comWills = $request->comWills;
        $address = $comWills['address'];
        $jobModule = $comWills['jobModule'];
        $job = $comWills['job'];
        $salary = $comWills['salary'];
        $workType = $comWills['workType'];
        $industry = $comWills['industry'];
        $jobTime = $comWills['jobTime'];
        $tags = $comWills['tags'];
        $dataArr = array(
            'address' => $address,
            'jobModule' => $jobModule,
            'job' => $job,
            'salary' => $salary,
            'workType' => $workType,
            'industry' => $industry,
            'jobTime' => $jobTime,
            'tags' => $tags,
        );
        $this->staffService->setComWill($uid,$dataArr);
        return $this->toJson(0);
    }

    /**
     * ---------------------------
     * 员工公司求职意向
     * ---------------------------
     */





    /**
     * ---------------------------
     * 员工工厂求职意向
     * ---------------------------
     */

    /**
     * 员工工厂求职意向，获取
     */
    public function getFacWills(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $model = $this->staffService->getFacWill($uid);
        if (!$model) { return $this->toJson(-3); }
        return $this->toJson(0,$model);
    }

    /**
     * 员工工厂求职意向，设置
     */
//    public function setFacWills(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
////            return $this->toJson(-1);
//            return redirect('/h5/error.html?errMsg=接口权限错误');
//        }
//        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
//        $viewJump = $request->viewJump;
//        if (!$token || !$request->fabricTypes || !$request->techLevel
//            || !$request->skills) {
////            return $this->toJson(-2);
//            return redirect('/h5/error.html?errMsg=参数错误');
//        }
//        $uid = $this->accessGet($token);
//        $fabrics = $request->fabricTypes;
//        $techLevel = $request->techLevel;
//        $skills = $request->skills;
//        $dataArr = array(
//            'fabricType' => $fabrics,
//            'techLevel' => $techLevel,
//            'skill' => $skills,
//        );
//        $this->staffService->setFacWill($uid,$dataArr);
////        return $this->toJson(0);
//        return redirect($viewJump);
//    }
    public function setFacWills(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $viewJump = $request->viewJump;
        if (!$token) {
            return redirect('/h5/error.html?errMsg=未登陆');
        }
        if (!$request->fabricTypes || !$request->techLevel
            || !$request->skills) {
            return redirect('/h5/error.html?errMsg=参数未选择');
        }
        $uid = $this->accessGet($token);
        $fabrics = $request->fabricTypes;
        $techLevel = $request->techLevel;
        $skills = $request->skills;
        $dataArr = array(
            'fabricType' => $fabrics,
            'techLevel' => $techLevel,
            'skill' => $skills,
        );
        $this->staffService->setFacWill($uid,$dataArr);
        return redirect('/h5/login/staffList.html');
    }

    /**
     * ---------------------------
     * 员工工厂求职意向
     * ---------------------------
     */





    /**
     * ---------------------------
     * 员工公司认证
     * ---------------------------
     */

    /**
     * 员工公司认证，获取
     */
    public function getComFiles(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $model = $this->staffService->getComFile($uid);
        if (!$model) { return $this->toJson(-3); }
        $dataArr = array(
            'selfs' => $model->selfs,
            'idcards' => $model->idcards,
            'byzs' => $model->byzs,
            'awards' => $model->awards,
            'marrys' => $model->marrys,
            'drives' => $model->drives,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 员工公司认证，设置
     * 待删除
     */
    public function setComFileUpload(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $viewJump = $request->viewJump;
        if (!$token) {
            return redirect('/h5/error.html?errMsg=用户未登录或未注册');
        }
        $uid = $this->accessGet($token);
        if (!isset($request->self) && !isset($request->idcard) &&
            !isset($request->byz) && !isset($request->award) &&
            !isset($request->marry) && !isset($request->drive)) {
            return redirect('/h5/error.html?errMsg=文件未上传');
        }
        if (isset($request->self)) {
            $resFile = Upload::uploadImgByApi($request,'self');
            $fileName = 'selfs';
        }
        else if (isset($request->idcard)) {
            $resFile = Upload::uploadImgByApi($request,'idcard');
            $fileName = 'idcards';
        }
        else if (isset($request->byz)) {
            $resFile = Upload::uploadImgByApi($request,'byz');
            $fileName = 'byzs';
        }
        else if (isset($request->award)) {
            $resFile = Upload::uploadImgByApi($request,'award');
            $fileName = 'awards';
        }
        else if (isset($request->marry)) {
            $resFile = Upload::uploadImgByApi($request,'marry');
            $fileName = 'marrys';
        }
        else if (isset($request->drive)) {
            $resFile = Upload::uploadImgByApi($request,'drive');
            $fileName = 'drives';
        }
        else {
            $fileName = '';
            return redirect('/h5/error.html?errMsg=未知错误');
        }
        if ($resFile['code']!=0) {
            return redirect('/h5/error.html?errMsg='.$resFile['msg']);
        }
        $this->staffService->setComFile($uid,$fileName,$resFile['filePath']);
        return redirect($viewJump);
    }

    /**
     * 员工公司认证图片，链接设置
     */
    public function setComFileLink(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $selfs = $request->selfs;
        $idcards = $request->idcards;
        $byzs = $request->byzs;
        $awards = $request->awards;
        $marrys = $request->marrys;
        $drives = $request->drives;
        if (!$selfs && !$idcards && !$byzs &&
            !$awards && !$marrys && !$drives) {
            $this->toJson(-3);
        }
        $dataArr = array(
            'selfs' => $selfs ? $selfs : '',
            'idcards' => $idcards ? $idcards : '',
            'byzs' => $byzs ? $byzs : '',
            'awards' => $awards ? $awards : '',
            'marrys' => $marrys ? $marrys : '',
            'drives' => $drives ? $drives : '',
        );
        $this->staffService->setComFileLink($uid,$dataArr);
        return $this->toJson(0);
    }

    /**
     * ---------------------------
     * 员工公司认证
     * ---------------------------
     */





    /**
     * ---------------------------
     * 员工职位
     * ---------------------------
     */

    /**
     * 工厂职位
     */
    public function getJobs(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        $jobService = new JobService();
        $models = $jobService->getJobs($cate);
        return $this->toJson(0,$models);
    }

    /**
     * 公司职位
     */

    /**
     * ---------------------------
     * 员工职位
     * ---------------------------
     */

    /**
     * 资料的完善度
     */
    public function getUserInfoPer(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $staff = $this->staffService->getOneByUid($uid);
        $staffOption = $this->staffService->getOptionByUid($uid);
//        $staffs = $this->object2array($staff);
//        $staffOptions = $this->object2array($staffOption);
        $dataArr = array( //百分比
            'type' => 0,
            'job' => 0,
            'basic' => 0,
            'ext' => 0,
            'will' => 0,
            'file' => 0,
        );
        //员工类型
        if ($staff) { $dataArr['type'] = 100; }
        //职位选择
        if ($staff && $staff->job) { $dataArr['job'] = 100; }
        //基本信息
//        $basics = array(
//            'realname','sex', 'birthday','education','weight','political_status','native_place', 'nationality','marriage','idcard','house','image', 'household_address','residential_address','contact_person','contact_phone', 'salary','skill','hobby',
//        );
//        $basicPer = 0;
//        foreach ($basics as $basic) {
//            if ($staffs && $staffs[$basic]) { $basicPer ++; }
//        }
        if ($staff && $staff->realname) { $dataArr['basic'] = 100; }
        //扩展信息
//        $exts = array(
//            'edu_bgs','work_historys','familys', 'comments',
//        );
//        $extPer = 0;
//        foreach ($exts as $ext) {
//            if ($staffOptions && $staffOption[$ext]) { $extPer ++; }
//        }
//        $dataArr['ext'] = $extPer / $exts;
        if ($staffOption) { $dataArr['ext'] = 100; }
        //求职意向
        if ($staffOption && ($staffOption->will_factorys ||
                $staffOption->will_companys)) {
            $dataArr['will'] = 100;
        }
        //员工认证
        if ($staffOption && $staffOption->files) {
            $dataArr['file'] = 100;
        }
        return $this->toJson(0,$dataArr);
    }
}